package com.grade.controller;
import com.grade.Beans.Student;
import com.grade.service.StudentService;
import com.grade.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/student")
public class StudentController {
    @Autowired
    StudentService studentService;

    @RequestMapping("/getAllStudent")
    @ResponseBody
    public Results<List<Student>> findAllStudent(@RequestParam(value = "studentName", defaultValue = "") String studentName,
                                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                                 @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("studentName", "%" + studentName + "%");
        map.put("startpage", (page - 1) * pageSize);
        map.put("pageSize", pageSize);
        List<Student> studentList = studentService.findByStudentPage(map);
        long count = studentService.findByStudent(map);
        Results<List<Student>> results = new Results<>("0", "查询成功", studentList, count);
        return results;
    }

    @RequestMapping("/addStudent")
    @ResponseBody
    public Results<Student> addStudent(Student student) {
        Student s = studentService.findByStudentNo(student.getStudentNo());
        Results<Student> results = null;
        if (s != null) {
            results = new Results<>("1", "该学号已经有了");
        } else {
            int count = studentService.addStudent(student);
            if (count == 1) {
                results = new Results<>("0", "新增成功");
            } else {
                results = new Results<>("2", "新增失败");
            }
        }
        return results;
    }

    @RequestMapping("/deleteStudent")
    @ResponseBody
    public Results<Student> deleteStudent(int studentNo) {
        int count = studentService.deleteStudent(studentNo);
        Results<Student> results = null;
        if (count == 1) {
            results = new Results<>("0", "删除成功");
        } else {
            results = new Results<>("1", "删除失败");
        }
        return results;
    }

    @RequestMapping("/updateStudent")
    @ResponseBody
    public Results<Student> updateStudent(Student student) {
        int count = studentService.updateStudent(student);
        Results<Student> results = null;
        if (count == 1) {
            results = new Results<>("0", "更新成功");
        } else {
            results = new Results<>("2", "更新失败");
        }
        return results;
    }
    @RequestMapping("/login")
    @ResponseBody
    public Results login(int userName, String password , String code, HttpSession session){
        String Systemcode=(String)session.getAttribute("systemCode");
        if(code.equalsIgnoreCase(Systemcode)){
             Student student=studentService.login(userName,password);
             if(student!=null)
             {
                 session.setAttribute("usr",student.getStudentName());
                 session.setAttribute("id",userName);
                 return  new Results("0","登录成功");
             }
             else{
                 return new Results("1","账号或密码错误");
             }
        }else{
 return new Results<>("4","验证码错误");
        }
    }
    @PostMapping("/studentSelect")
    @ResponseBody
    public Results<List<Student>>studentSelect(){
        List<Student> studentList=studentService.FindAll();
        return  new Results<>("0",null,studentList,null);
    }
    @RequestMapping("/updatePwd")
@ResponseBody
    public Results updatePwd(String pwd, String pwd1, String pwd2, HttpSession session) {
        // 1. 非空校验，避免空指针异常
        if (pwd == null || pwd1 == null || pwd2 == null) {
            return new Results("1", "密码不能为空");
        }

        // 2. 获取当前登录教师姓名
        String tname = (String) session.getAttribute("usr");
        int id=(Integer) session.getAttribute("id");
        if (tname == null) {
            return new Results("1", "请先登录");
        }

        // 3. 验证两次新密码是否一致
        if (!pwd1.equals(pwd2)) {
            return new Results("2", "两次输入的新密码不同");
        }

        // 4. 验证原密码是否正确（新增必要步骤）
        Student student = studentService.login(id,pwd);
        if (student == null) {
            return new Results("3", "原密码不正确");
        }

        // 5. 执行密码更新
        int count = studentService.updatepassword(id, pwd1);
        if (count > 0) {
            // 6. 密码修改成功后建议清除session，强制重新登录
            session.invalidate();
            return new Results("0", "密码修改成功");
        } else {
            return new Results("1", "密码修改失败");
        }
    }
}

